layui.config({
    base : "js/information/"
}).use(['form','layer','jquery','laypage','laydate'],function(){
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        laydate = layui.laydate,
        $ = layui.jquery;

    var pageSize = 3;
    var currPage = 0;

    initList();

    function initList(){
        var title = $("#dramaName").val().replace(/(^\s*)|(\s*$)/g,"");
        var startTime = $("#startTime").val().replace(/(^\s*)|(\s*$)/g,"");
        var endTime = $("#endTime").val().replace(/(^\s*)|(\s*$)/g,"");

        if(startTime) startTime = startTime + " 00:00:00";
        if(endTime) endTime = endTime + " 23:59:59";

        // 页面初始化
        var data = {page:currPage,pageSize:pageSize,title:title,startTime:startTime,endTime:endTime};
        $.ajax({
            type: 'POST',
            dataType : "text",
            url: "/drama/getDramaList",
            data: data,
            success: function(data){
                var data = data.replace(/id\":(\d+),/g,"id\":\"$1\",");
                data = $.parseJSON(data);
                var html = template('dramaList', data);
                document.getElementById('dramaListTb').innerHTML = html;
                if(currPage >= 0) initPage(data["total"]);
            },error: function(){
                console.log("请求失败");
            }
        });
    }

    //查询
    $("#searchBtn").click(function(e){

        e.preventDefault();
        e.stopPropagation();

        var title = $("#dramaName").val().replace(/(^\s*)|(\s*$)/g,"");
        var startTime = $("#startTime").val().replace(/(^\s*)|(\s*$)/g,"");
        var endTime = $("#endTime").val().replace(/(^\s*)|(\s*$)/g,"");

        if(startTime) startTime = startTime + " 00:00:00";
        if(endTime) endTime = endTime + " 23:59:59";

        //if(title != '' && typeof title != undefined){
        var index = layer.msg('查询中,请稍候',{icon: 16,time:false,shade:0.8});

        currPage = 0;
        var data = {page:currPage,pageSize:pageSize,title:title,startTime:startTime,endTime:endTime};
        $.ajax({
            cache: false,
            dataType: "json",
            async: false,
            type: 'POST',
            url: "/drama/getDramaList",
            data: data,
            success: function(res){
                var html = template('dramaList', res);
                document.getElementById('dramaListTb').innerHTML = html;
                if(currPage >= 0) initPage(res["total"]);
                layer.close(index);
            },
            error: function(){
                layui.layer.tips('查询失败,请重试', '.layui-layer-setwin .layui-layer-close', {tips: 3});
                layer.close(index);
            }
        });

        // setTimeout(function(){
        //     layer.close(index);
        // },2000);
        //}else{
        //layer.msg("请输入标题");
        //}
    });

    $("body").on("click","#operate>.dramaCheck",function (e) {
        e.preventDefault();
        var id = $(this).attr("dramaId");
        var status = $(this).attr("dramaStatus");

        var data = {"id":id,"status":status};
        $.ajax({
            type: 'POST',
            dataType : "json",
            url: "/drama/updateDramaStatus",
            data: data,
            success: function(res){
                if(res["result"] == "success"){
                    initList();
                    layer.msg("状态更新成功");
                }
            },error: function(){
                layer.msg("状态更新失败,请重试");
            }
        });
    });

    // 分页
    function initPage(total){
        console.log(">>>>>>>>>>>initPage<<<<<<<<<<<");

        laypage({
            cont : "pagination",
            pages : Math.ceil(total/pageSize),
            curr : currPage + 1,
            skip : true,
            jump : function(obj,first){
                // $(".news_content").html(renderDate(newsData,obj.curr));
                // $('.news_list thead input[type="checkbox"]').prop("checked",false);
                // form.render();

                currPage = obj.curr - 1;
                console.log("*********11111111111111");
                if(!first){
                    initList();
                    form.render();
                }else{
                    form.render();
                }
            }
        })
    }
})
